package leetcode.leetcode_1329;

import java.util.ArrayList;

class Solution {
    private int [][] ans ;
    private int m ;
    private int n ;
    public int[][] diagonalSort(int[][] mat) {
        n = mat.length;
        m = mat[0].length;
        ans = new int[n][m];
        for (int i =0 ;i<n;i++){
            sort(i,0,mat);
        }
        for (int j =1 ;j<m;j++){
            sort(0,j,mat);
        }
        return ans;
    }
    public void sort(int i ,int j ,int [][] mat){
        int i_temp = i;
        int j_temp = j;
        ArrayList<Integer> temp = new ArrayList<>();
        while(i_temp<n&&j_temp<m){
            temp.add(mat[i_temp][j_temp]);
            i_temp++;
            j_temp++;
        }
        temp.sort((x,y)->Integer.compare(x,y));
        int index = 0;
        while(i<n&&j<m){
            ans[i][j] = temp.get(index);
            i++;
            j++;
            index++;
        }
    }

}